package code.extract;

import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CodePageDealer extends BasePageDealer{
	public static void main(String args[])
	{
		try {
			String text = FileDealer.getFileContent("testpage\\sample_page\\7_csdnblog.txt", "GBK");
			text = CodePageDealer.eraseOneTagAndContent(text, "head");
			text = CodePageDealer.eraseOneTagAndContent(text, "script");
			text = CodePageDealer.eraseOneTagAndContent(text, "style");
			ArrayList<String> tagList = new ArrayList<String>();
			tagList.add("code");
			tagList.add("pre");
			tagList.add("div");
			tagList.add("p");
			tagList.add("td");
			ArrayList<String> tList = getTextFragmentInPage(text, tagList);
			for (int i=0; i<tList.size(); ++i)
			{
				if (!CodeJudge.isCode(tList.get(i)))
				{
					continue;
				}
				System.out.println(i + "-----------------");
				System.out.println(HTMLEntityTransform.getResultString(tList.get(i)));
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static ArrayList<String> getTextFragmentInOneTag(String originString, String tag)
	{
		ArrayList<String> resultList = new ArrayList<String>();
		ArrayList<String> rawList = extractOneTagContent(originString, tag);
		for (int i=0; i<rawList.size(); ++i)
		{
			if (rawList.get(i) != null)
			{
				String thisFragment = eraseAllTags(rawList.get(i));
				resultList.add(new String(thisFragment));
			}
		}
		return resultList;
	}
	
	public static ArrayList<String> getTextFragmentInPage(String originString, ArrayList<String> segTagList)
	{
		ArrayList<String> resultList = new ArrayList<String>();
		ArrayList<Integer> segPosList = new ArrayList<Integer>();
		StringBuffer sb = new StringBuffer();
		for (int i=0; i<segTagList.size(); ++i)
		{
			if (i != 0)
			{
				sb.append("|");
			}
			sb.append(segTagList.get(i));
		}
		String str_pattern = "<(/{0,1})(\\s*?)(" + sb.toString() + ")((\\s*?)|(\\s+?.*?))(/{0,1})>";
//		System.out.println(str_pattern);
		Pattern pattern = Pattern.compile(str_pattern, Pattern.DOTALL
					| Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(originString);
		while (matcher.find())
		{
			segPosList.add(new Integer(matcher.start()));
//			System.out.println(matcher.start() + ":" + new String(originString.substring(matcher.start(), matcher.end())));
		}
		for (int i=0; i<segPosList.size(); ++i)
		{
			if ( i < segPosList.size() - 1)
			{
				String rawText = new String(originString.substring(segPosList.get(i), segPosList.get(i+1)));
				resultList.add(new String(eraseAllTags(rawText)));
			}
			else
			{
				String rawText = new String(originString.substring(segPosList.get(i)));
				resultList.add(new String(eraseAllTags(rawText)));
			}
		}
		return resultList;
	}
}